package com.ruoyi.web.controller.borehole;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.web.domain.query.AddReportQuery;
import com.ruoyi.web.domain.query.ReportPageQuery;
import com.ruoyi.web.domain.vo.ReportPageVO;
import com.ruoyi.web.service.BhReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;

@Slf4j
@Tag(name = "作业报备")
@RestController
@RequestMapping("/jobReport")
public class BhReportController {

    @Autowired
    private BhReportService bhReportService;

    @Operation(summary = "作业报备列表页 - 分页查询")
    @GetMapping("/getReportPage")
    public TableDataInfo<ReportPageVO> getReportPage(@Validated(QueryGroup.class) ReportPageQuery reportPageQuery, PageQuery pageQuery) {
        return bhReportService.getReportPage(reportPageQuery, pageQuery);
    }

    @Operation(summary = "新增作业报备")
    @RepeatSubmit
    @PostMapping("/addReport")
    public R<Void> addReport(@RequestBody AddReportQuery addReportQuery) {
        return bhReportService.addReport(addReportQuery);
    }

    @Operation(summary = "删除作业报备")
    @RepeatSubmit
    @GetMapping("/delReport")
    public R<Void> delReport(@RequestParam("id") Integer id) {
        return bhReportService.delReport(id);
    }

    @Operation(summary = "编辑作业报备")
    @RepeatSubmit
    @PostMapping("/modReport")
    public R<Void> modReport(@RequestBody AddReportQuery addReportQuery) {
        return bhReportService.modReport(addReportQuery);
    }

    @Operation(summary = "导出作业报备")
    @RepeatSubmit
    @GetMapping("/exportReport")
    public void exportExcel(HttpServletResponse response, @Validated(QueryGroup.class) ReportPageQuery reportPageQuery, PageQuery pageQuery) {
        try (OutputStream out = response.getOutputStream()) {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("作业报备导出数据表", "UTF-8").replace("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            bhReportService.exportExcel(out, reportPageQuery, pageQuery);
            out.flush();
        } catch (Exception e) {
            log.error("exportExcel error:{}", e.getMessage());
        }
    }

    @Operation(summary = "获取作业报备详情")
    @GetMapping("/getReportDetail")
    public R<ReportPageVO> getReportDetail(@RequestParam("id") Integer id) {
        return bhReportService.getReportDetail(id);
    }
}
